package com.jungle.client.channel;

import com.google.gson.Gson;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import java.io.File;
import java.util.Map;

public class RequestHelper {
    private RequestHelper() {
    }

    private static final Gson GSON = new Gson();

    /**
     * 将JSON字符串转换为RequestBody对象
     * 用于HTTP请求中发送JSON数据
     *
     * @param jsonStr JSON字符串
     * @return RequestBody对象，用于HTTP请求
     */
    public static RequestBody toRequestBody(String jsonStr) {
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RequestBody.create(jsonStr, mediaType);
    }

    public static RequestBody toRequestBody(Object param) {
        String string = GSON.toJson(param);
        return toRequestBody(string);
    }

    /**
     * 将文件转换为RequestBody对象
     * 用于HTTP请求中发送文件数据
     *
     * @param file 文件对象
     * @return RequestBody对象，用于HTTP请求
     */
    public static RequestBody toRequestBody(File file) {
        return RequestBody.create(file, MediaType.parse("application/octet-stream"));
    }

    /**
     * 将字节数组转换为RequestBody对象
     * 用于HTTP请求中发送二进制数据
     *
     * @param bytes 字节数组
     * @return RequestBody对象，用于HTTP请求
     */
    public static RequestBody toRequestBody(byte[] bytes) {
        return RequestBody.create(bytes, MediaType.parse("application/octet-stream"));
    }

    /**
     * 将键值对参数转换为FormBody对象
     * 用于HTTP请求中的表单数据提交
     *
     * @param params 键值对参数
     * @return FormBody对象，用于HTTP请求
     */
    public static FormBody.Builder toFormBody(Map<String, String> params) {
        FormBody.Builder builder = new FormBody.Builder();
        params.forEach(builder::add);
        return builder;
    }

    /**
     * 将单个键值对转换为FormBody对象
     * 用于HTTP请求中的表单数据提交
     *
     * @param key   键
     * @param value 值
     * @return FormBody对象，用于HTTP请求
     */
    public static FormBody.Builder toFormBody(String key, String value) {
        return new FormBody.Builder()
                .add(key, value);
    }

    /**
     * 将单个键值对参数转换为MultipartBody对象
     * 用于HTTP请求中的multipart/form-data类型的数据提交
     *
     * @param key   键
     * @param value 值
     * @return MultipartBody对象，用于HTTP请求
     */
    public static MultipartBody.Builder toMultipartBody(String key, String value) {
        return new MultipartBody.Builder()
                .addFormDataPart(key, value);
    }

    /**
     * 将键值对参数转换为MultipartBody对象
     * 用于HTTP请求中的multipart/form-data类型的数据提交
     *
     * @param params 键值对参数
     * @return MultipartBody对象，用于HTTP请求
     */
    public static MultipartBody.Builder toMultipartBody(Map<String, String> params) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        params.forEach(builder::addFormDataPart);
        return builder;
    }

    public static MultipartBody.Builder toMultipartBody(Map<String, String> params, Map<String, File> files) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        params.forEach(builder::addFormDataPart);
        files.forEach((key, file) -> builder.addFormDataPart(key, file.getName(), toRequestBody(file)));
        return builder;
    }
}
